package controller.handstates;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import model.Deck;
import model.HandChain;
import model.Player;
import model.GameChain;
import model.Table;
import model.timers.BlindCounter;

public class PreFlopState extends AbstractState {
	
	private BlindCounter blinds;
	private Deck deck;
		

	public PreFlopState(HandChain playerChain, Table table, BlindCounter blinds, Deck deck){		
		super(playerChain, table, blinds);
		this.blinds = blinds;
		this.deck = deck;
		
	}
	
	@Override
	public void playState() {
		System.out.println("*Round Preflop***");
		doBlinds(blinds);	
		handOutHands();	
		playRound();
	}

	
	
	@Override
	public AbstractState getNextState() {
		AbstractState state = null;
		if(!isHandOver()){
			state = new FlopState(handChain, table, deck, blinds);	
		}
		return state;
	}
	
	
	private void handOutHands(){
		int cardsToDealOut = handChain.size() * 2;
		for(int i=0; i < cardsToDealOut; i++){
			Player p = handChain.getNextPlayer();
			p.dealCard(deck.drawCard());
		}
		
		for(Player p : handChain.getPlayers()){
			System.out.println("Player"+p.getSeat()+" has: " + p.getHand().toString());
		}
	}
	
	
	protected boolean roundOver() {
		boolean roundOver = false;
		// round is over once we have gone full circle or one player is left.
		if(handChain.getActivePlayerSeat() == handChain.getLastActionPlayerSeat()  ){
			System.out.println("Round is over due to full circle");
			roundOver = true;
		}
		
		if( isHandOver() ){
			roundOver = true;
			System.out.println("Round is over due to hand being over");
		}
		
		// last action = -1 means no one raised.  if activeplayer is the person after the big blind
		// the round is over
		if( handChain.getLastActionPlayerSeat() == -1 && handChain.getActivePlayerSeat() == handChain.getBigBlindPlayerSeat()  ){
			roundOver = true;
			System.out.println("Round is over due to full circle preflop");
		}
		
		return roundOver;
	}
	
	/**
	 * Attempts to take big and small blinds from players. Put the amount they were able to contribute
	 * in the middle. Then leaves the active player the one after the big blind
	 * @param blinds
	 */
	private void doBlinds(BlindCounter blinds){
		int bigBlind = blinds.getBigBlind();;
		int smallBlind = blinds.getSmallBlind();
		
		int smallBlindContribution = 0;
		int bigBlindContribution = 0;
		
		// Special case, only dealer and an opponent are left
		Player small = null;
		Player big = null;
		if(handChain.size()<=2){
			big = handChain.getNextPlayer();
			small = handChain.getNextPlayer();
		}else{
			small = handChain.getNextPlayer();
			big = handChain.getNextPlayer();			
		}
		handChain.getNextPlayer();
		
		bigBlindContribution = big.removeChips(bigBlind);
		smallBlindContribution = small.removeChips(smallBlind);
		System.out.println("Small blind: Seat " + small.getSeat() + " : Chips " + smallBlindContribution);
		System.out.println("Big blind: Seat " + big.getSeat() + " : Chips " + bigBlindContribution);
		
		table.addChips(smallBlindContribution + bigBlindContribution);
		System.out.println("Chips in middle: " + table.getChips());
		
		
	}
}
